package com.wisdudu.blank.ui.login;

import android.content.Intent;
import android.databinding.ObservableField;
import android.text.TextUtils;

import com.kelin.mvvmlight.base.ViewModel;
import com.kelin.mvvmlight.command.ReplyCommand;
import com.orhanobut.logger.Logger;
import com.wisdudu.blank.data.Injection;
import com.wisdudu.blank.data.bean.UserInfo;
import com.wisdudu.blank.data.repo.UserRepo;
import com.wisdudu.blank.data.source.remote.client.retrofit.subscribers.ProgressSubscriber;
import com.wisdudu.blank.data.source.remote.client.retrofit.subscribers.SubscriberOnNextListener;
import com.wisdudu.blank.databinding.FragmentLoginBinding;
import com.wisdudu.blank.support.util.ToastUtil;
import com.wisdudu.blank.support.widget.loading.LoadingProgressDialog;
import com.wisdudu.blank.ui.home.HomeActivity;
import com.wisdudu.frame.support.util.RegularUtil;

/**
 * Created by qyf on 2016/8/11.
 */

public class LoginVM implements ViewModel {
    private LoginFragment mFragment;
    //data
    public final ObservableField<String> phoneNum = new ObservableField<>();
    public final ObservableField<String> password = new ObservableField<>();
    private UserRepo mUserRepo;
    private FragmentLoginBinding mBinding;

    public LoginVM(LoginFragment fragment, FragmentLoginBinding mBinding) {
        mFragment = fragment;
        mUserRepo = Injection.provideUserRepo();

        String phoneNum = mUserRepo.getPhoneNum();
        mBinding.loginPhoneNumEdt.setText(phoneNum);
        this.phoneNum.set(phoneNum);
        this.mBinding = mBinding;
//        mBinding.loginPhoneNumEdt.setSelection(phoneNum.get().length());
//        loadCacheUser();
    }

    private void loadCacheUser() {
        password.set("123456");
        toLogin();
    }

    public String getSectionStr() {
        return mBinding.loginPhoneNumEdt.getText().toString();
//        return phoneNum.get();
    }

    public ReplyCommand onLoginClick = new ReplyCommand(() -> {
        toLogin();
    });

    public ReplyCommand onClearClick = new ReplyCommand(() -> {
        phoneNum.set("");
    });

    public ReplyCommand onRegisterClick = new ReplyCommand(() -> {
        mFragment.startActivity(new Intent(mFragment.getActivity(), RegisterActivity.class));
    });

    public ReplyCommand onForgetPassClick = new ReplyCommand(() -> {
        mFragment.addFragment(new ForgetPasswordFragment());
    });

    private void toLogin() {
        if (TextUtils.isEmpty(phoneNum.get())) {
            ToastUtil.INSTANCE.toast("手机号码不能为空");
            return;
        }
        if (!RegularUtil.isMobile(phoneNum.get())) {
            ToastUtil.INSTANCE.toast("手机号码格式错误");
            return;
        }
        if (TextUtils.isEmpty(password.get())) {
            ToastUtil.INSTANCE.toast("密码不能为空");
            return;
        }
        if (!RegularUtil.isPassword(password.get())) {
            ToastUtil.INSTANCE.toast("请输入6-20位字母或数字密码");
            return;
        }
        mUserRepo.login(phoneNum.get(), password.get())
                .compose(mFragment.bindToLifecycle())
                .subscribe(new ProgressSubscriber<>(new SubscriberOnNextListener<UserInfo>() {
                    @Override
                    public void onNext(UserInfo userInfo, LoadingProgressDialog pd) {
//                        ICVSSUserModule.getInstance(mFragment.mActivity).getIcvss().equesLogin(mFragment.mActivity, ICVSSUserModule.DISTRIBUTE_URL, ICVSSUserModule.USERNAME, ICVSSUserModule.APPKEY);
                        mFragment.mActivity.startActivity(new Intent(mFragment.getActivity(), HomeActivity.class));
                        mFragment.mActivity.finish();
                    }
                }, mFragment.mActivity, "正在登录..."));
        Logger.d("点击登录按钮,%s,%s", phoneNum.get(), password.get());
    }

    public void onDestroy() {
        mUserRepo.closeRealm();
    }
}
